package com.joplin.iemsdemo.service

import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.graphics.PixelFormat
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import com.joplin.iemsdemo.R
import com.joplin.iemsdemo.manager.IEMSManager

class IEMSService : Service() {
    val windowManager by lazy { getSystemService(WINDOW_SERVICE) as WindowManager }
    var view: View? = null

    inner class IEMSBinder : Binder() {
        fun getService() = this@IEMSService
    }

    override fun onBind(intent: Intent): IBinder {
        return IEMSBinder()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    @SuppressLint("MissingPermission")
    override fun onCreate() {
        super.onCreate()

        //创建前台常驻通知
        val channelID = "iems"
        val channelName = "耳返服务"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
            val channelManager = getSystemService(NotificationManager::class.java)
            channelManager.createNotificationChannel(channel)
        }
        val notificationBuilder = NotificationCompat.Builder(this, channelID)
        notificationBuilder.setContentText("标题：耳返服务")
        val notification = notificationBuilder.build()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
        }else{
            startForeground(1, notification)
        }

        val params = WindowManager.LayoutParams()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        }else{
            params.type = WindowManager.LayoutParams.TYPE_PHONE
        }
        params.gravity = Gravity.TOP or Gravity.END
        params.width = WindowManager.LayoutParams.WRAP_CONTENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        params.format = PixelFormat.RGBA_8888
        params.x = -50
        params.y = 50
        params.flags =
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        view = View.inflate(this, R.layout.window_iems, null)
        view!!.findViewById<Button>(R.id.tvIEMSStart).setOnClickListener {
            IEMSManager.start()
        }
        view!!.findViewById<Button>(R.id.tvIMESStop).setOnClickListener {
            IEMSManager.stop()
        }
        windowManager.addView(view, params)
    }

    override fun onDestroy() {
        super.onDestroy()
        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        if (view != null) {
            windowManager.removeView(view)
        }
    }
}